home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / CommonSample / MyErrorHandler.c < prev    next >
Encoding:
Text File  |  1996-02-19  |  14.8 KB  |  370 lines  |  [TEXT/MPCC]

  1. // Quickdraw 3D sample code
  2. //
  3. // Nick Thompson, AppleLink: DEVSUPPORT (devsupport@applelink.apple.com)
  4. //
  5. // ©1994-5 Apple Computer Inc., All Rights Reserved
  6.  
  7. #include <Dialogs.h>
  8. #include <Memory.h>
  9. #include <SegLoad.h>
  10. #include <TextUtils.h>
  11.  
  12. #include "QD3D.h"
  13. #include "QD3DErrors.h"
  14.  
  15. #include "MyErrorHandler.h"
  16.  
  17.  
  18. //---------------------------------------------------------------------------
  19.  
  20. const int kErrorHandlerAlertID = 21032 ;            // ID of the alert resource
  21.  
  22. //---------------------------------------------------------------------------
  23.  
  24. // THIS ENUM MUST MATCH THE STRUCT BELOW
  25. enum {
  26.     kWarningString,
  27.     kErrorString,
  28.     kFirstErrorString,
  29.     kLastErrorString,
  30.     kSpaceString,
  31.     kOpenParen,
  32.     kCloseParen,
  33.     kUnknownErrorString
  34. } ;
  35.  
  36.  
  37. // use these to construct intelligable messages
  38. static struct {
  39.     Str255        errorString ;
  40. } stringTable[] = {
  41.     "\pQuickDraw 3D warning.\r\r", 
  42.     "\pQuickDraw 3D Error.\r\r",
  43.     "\p\rFirst Error: ",
  44.     "\p\rLast Error: "
  45.     "\p ",
  46.     "\p(",
  47.     "\p)",
  48.     "\p\rUnknown Message"
  49. } ;
  50.  
  51.  
  52. //---------------------------------------------------------------------------
  53.  
  54.  
  55. /*
  56.     To generate these tables, copy/paste the error codes from QD3DErrors.h
  57.     and use this regexp:
  58.     
  59.     Find:        kQ3Error([0-9A-z]*),
  60.     Replace:    kQ3Error\1, "\\pkQ3Error\1",
  61.     
  62.     Find:        kQ3Warning([0-9A-z]*),
  63.     Replace:    kQ3Warning\1, "\\pkQ3Warning\1",
  64.     
  65.     Find:        kQ3Notice([0-9A-z]*),
  66.     Replace:    kQ3Notice\1, "\\pkQ3Notice\1",
  67.     
  68. */
  69.  
  70.  
  71. // define a table for all of the QuickDraw 3D error codes
  72. struct {
  73.     TQ3Warning    theWarning ;
  74.     Str255        theString ;
  75. } warningTable[] = {
  76.     /* General System */
  77.     kQ3WarningInternalException, "\pkQ3WarningInternalException",    
  78.     /* Object Warnings */
  79.     kQ3WarningNoObjectSupportForDuplicateMethod, "\pkQ3WarningNoObjectSupportForDuplicateMethod",
  80.     kQ3WarningNoObjectSupportForDrawMethod, "\pkQ3WarningNoObjectSupportForDrawMethod",
  81.     kQ3WarningNoObjectSupportForWriteMethod, "\pkQ3WarningNoObjectSupportForWriteMethod",
  82.     kQ3WarningNoObjectSupportForReadMethod, "\pkQ3WarningNoObjectSupportForReadMethod",
  83.     kQ3WarningUnknownElementType, "\pkQ3WarningUnknownElementType",
  84.     kQ3WarningTypeAndMethodAlreadyDefined, "\pkQ3WarningTypeAndMethodAlreadyDefined",
  85.     kQ3WarningTypeIsOutOfRange, "\pkQ3WarningTypeIsOutOfRange",
  86.     kQ3WarningTypeHasNotBeenRegistered, "\pkQ3WarningTypeHasNotBeenRegistered",
  87.     /* Parameter Warnings */
  88.     kQ3WarningVector3DNotUnitLength, "\pkQ3WarningVector3DNotUnitLength",
  89.     /* IO Warnings */
  90.     kQ3WarningInvalidSubObjectForObject, "\pkQ3WarningInvalidSubObjectForObject",
  91.     kQ3WarningInvalidHexString, "\pkQ3WarningInvalidHexString",
  92.     kQ3WarningUnknownObject, "\pkQ3WarningUnknownObject",
  93.     kQ3WarningInvalidMetafileObject, "\pkQ3WarningInvalidMetafileObject",
  94.     kQ3WarningUnmatchedBeginGroup, "\pkQ3WarningUnmatchedBeginGroup",
  95.     kQ3WarningUnmatchedEndGroup, "\pkQ3WarningUnmatchedEndGroup",
  96.     kQ3WarningInvalidTableOfContents, "\pkQ3WarningInvalidTableOfContents",
  97.     kQ3WarningUnresolvableReference, "\pkQ3WarningUnresolvableReference",
  98.     kQ3WarningNoAttachMethod, "\pkQ3WarningNoAttachMethod",
  99.     kQ3WarningInconsistentData, "\pkQ3WarningInconsistentData",
  100.     kQ3WarningReadLessThanSize, "\pkQ3WarningReadLessThanSize",
  101.     kQ3WarningFilePointerResolutionFailed, "\pkQ3WarningFilePointerResolutionFailed",
  102.     kQ3WarningFilePointerRedefined, "\pkQ3WarningFilePointerRedefined",
  103.     kQ3WarningStringExceedsMaximumLength, "\pkQ3WarningStringExceedsMaximumLength",
  104.     /* Memory Warnings */
  105.     kQ3WarningLowMemory, "\pkQ3WarningLowMemory",
  106.     kQ3WarningPossibleMemoryLeak, "\pkQ3WarningPossibleMemoryLeak",
  107.     /* View Warnings */
  108.     kQ3WarningViewTraversalInProgress, "\pkQ3WarningViewTraversalInProgress",
  109.     kQ3WarningNonInvertibleMatrix, "\pkQ3WarningNonInvertibleMatrix",
  110.     /* Quaternion Warning */
  111.     kQ3WarningQuaternionEntriesAreZero, "\pkQ3WarningQuaternionEntriesAreZero",
  112.     /* Renderer Warning */
  113.     kQ3WarningFunctionalityNotSupported, "\pkQ3WarningFunctionalityNotSupported",
  114.     /* DrawContext Warning */
  115.     kQ3WarningInvalidPaneDimensions, "\pkQ3WarningInvalidPaneDimensions",
  116.     /* Pick Warning */
  117.     kQ3WarningPickParamOutside, "\pkQ3WarningPickParamOutside",
  118.     /* Scale Warnings */
  119.     kQ3WarningScaleEntriesAllZero, "\pkQ3WarningScaleEntriesAllZero",
  120.     kQ3WarningScaleContainsNegativeEntries, "\pkQ3WarningScaleContainsNegativeEntries",
  121.     /* Generic Warnings */
  122.     kQ3WarningParameterOutOfRange, "\pkQ3WarningParameterOutOfRange",
  123. } ;
  124.  
  125.  
  126. struct {
  127.     TQ3Error    theError ;
  128.     Str255        theString ;
  129. } errorTable[] = {
  130.     /* Fatal Errors */
  131.     kQ3ErrorInternalError, "\pkQ3ErrorInternalError",
  132.     kQ3ErrorNoRecovery, "\pkQ3ErrorNoRecovery",                    /* TODO: This is not used correctly */
  133.     kQ3ErrorLastFatalError, "\pkQ3ErrorLastFatalError",
  134.     /* System Errors */
  135.     kQ3ErrorNotInitialized, "\pkQ3ErrorNotInitialized",
  136.     kQ3ErrorAlreadyInitialized, "\pkQ3ErrorAlreadyInitialized",
  137.     kQ3ErrorUnimplemented, "\pkQ3ErrorUnimplemented",
  138.     kQ3ErrorRegistrationFailed, "\pkQ3ErrorRegistrationFailed",
  139.     /* OS Errors */
  140.     kQ3ErrorUnixError, "\pkQ3ErrorUnixError",
  141.     kQ3ErrorMacintoshError, "\pkQ3ErrorMacintoshError",
  142.     kQ3ErrorX11Error, "\pkQ3ErrorX11Error",
  143.     /* Memory Errors */
  144.     kQ3ErrorMemoryLeak, "\pkQ3ErrorMemoryLeak",
  145.     kQ3ErrorOutOfMemory, "\pkQ3ErrorOutOfMemory",
  146.     /* Parameter errors */
  147.     kQ3ErrorNULLParameter, "\pkQ3ErrorNULLParameter",
  148.     kQ3ErrorParameterOutOfRange, "\pkQ3ErrorParameterOutOfRange",
  149.     kQ3ErrorInvalidParameter, "\pkQ3ErrorInvalidParameter",            /* TODO: Make this more specific */
  150.     kQ3ErrorInvalidData, "\pkQ3ErrorInvalidData",                /* TODO: Is this used? */
  151.     kQ3ErrorAcceleratorAlreadySet, "\pkQ3ErrorAcceleratorAlreadySet",        /* TODO: Is this used? */
  152.     kQ3ErrorVector3DNotUnitLength, "\pkQ3ErrorVector3DNotUnitLength",
  153.     kQ3ErrorVector3DZeroLength, "\pkQ3ErrorVector3DZeroLength",
  154.     /* Object Errors */
  155.     kQ3ErrorInvalidObject, "\pkQ3ErrorInvalidObject",
  156.     kQ3ErrorInvalidObjectClass, "\pkQ3ErrorInvalidObjectClass",
  157.     kQ3ErrorInvalidObjectType, "\pkQ3ErrorInvalidObjectType",
  158.     kQ3ErrorInvalidObjectName, "\pkQ3ErrorInvalidObjectName",
  159.     kQ3ErrorObjectClassInUse, "\pkQ3ErrorObjectClassInUse",            
  160.     kQ3ErrorAccessRestricted, "\pkQ3ErrorAccessRestricted",
  161.     kQ3ErrorMetaHandlerRequired, "\pkQ3ErrorMetaHandlerRequired",
  162.     kQ3ErrorNeedRequiredMethods, "\pkQ3ErrorNeedRequiredMethods",
  163.     kQ3ErrorNoSubClassType, "\pkQ3ErrorNoSubClassType",
  164.     kQ3ErrorUnknownElementType, "\pkQ3ErrorUnknownElementType",
  165.     kQ3ErrorNotSupported, "\pkQ3ErrorNotSupported",
  166.     /* Extension Errors */
  167.     kQ3ErrorNoExtensionsFolder, "\pkQ3ErrorNoExtensionsFolder",
  168.     kQ3ErrorExtensionError, "\pkQ3ErrorExtensionError",
  169.     kQ3ErrorPrivateExtensionError, "\pkQ3ErrorPrivateExtensionError",
  170.     /* Geometry Errors */
  171.     kQ3ErrorDegenerateGeometry, "\pkQ3ErrorDegenerateGeometry",
  172.     kQ3ErrorGeometryInsufficientNumberOfPoints, "\pkQ3ErrorGeometryInsufficientNumberOfPoints",
  173.     /* IO Errors */
  174.     kQ3ErrorNoStorageSetForFile, "\pkQ3ErrorNoStorageSetForFile",
  175.     kQ3ErrorEndOfFile, "\pkQ3ErrorEndOfFile",
  176.     kQ3ErrorFileCancelled, "\pkQ3ErrorFileCancelled",
  177.     kQ3ErrorInvalidMetafile, "\pkQ3ErrorInvalidMetafile",
  178.      kQ3ErrorInvalidMetafilePrimitive, "\pkQ3ErrorInvalidMetafilePrimitive",
  179.      kQ3ErrorInvalidMetafileLabel, "\pkQ3ErrorInvalidMetafileLabel",
  180.      kQ3ErrorInvalidMetafileObject, "\pkQ3ErrorInvalidMetafileObject",
  181.      kQ3ErrorInvalidMetafileSubObject, "\pkQ3ErrorInvalidMetafileSubObject",
  182.     kQ3ErrorInvalidSubObjectForObject, "\pkQ3ErrorInvalidSubObjectForObject",
  183.     kQ3ErrorUnresolvableReference, "\pkQ3ErrorUnresolvableReference",
  184.     kQ3ErrorUnknownObject, "\pkQ3ErrorUnknownObject",
  185.     kQ3ErrorStorageInUse, "\pkQ3ErrorStorageInUse",
  186.     kQ3ErrorStorageAlreadyOpen, "\pkQ3ErrorStorageAlreadyOpen",
  187.     kQ3ErrorStorageNotOpen, "\pkQ3ErrorStorageNotOpen",
  188.     kQ3ErrorStorageIsOpen, "\pkQ3ErrorStorageIsOpen",
  189.     kQ3ErrorFileAlreadyOpen, "\pkQ3ErrorFileAlreadyOpen",
  190.     kQ3ErrorFileNotOpen, "\pkQ3ErrorFileNotOpen",
  191.     kQ3ErrorFileIsOpen, "\pkQ3ErrorFileIsOpen",
  192.     kQ3ErrorBeginWriteAlreadyCalled, "\pkQ3ErrorBeginWriteAlreadyCalled",
  193.     kQ3ErrorBeginWriteNotCalled, "\pkQ3ErrorBeginWriteNotCalled",
  194.     kQ3ErrorEndWriteNotCalled, "\pkQ3ErrorEndWriteNotCalled",
  195.     kQ3ErrorReadStateInactive, "\pkQ3ErrorReadStateInactive",
  196.     kQ3ErrorStateUnavailable, "\pkQ3ErrorStateUnavailable",
  197.     kQ3ErrorWriteStateInactive, "\pkQ3ErrorWriteStateInactive",
  198.     kQ3ErrorSizeNotLongAligned, "\pkQ3ErrorSizeNotLongAligned",
  199.     kQ3ErrorFileModeRestriction, "\pkQ3ErrorFileModeRestriction",
  200.     kQ3ErrorInvalidHexString, "\pkQ3ErrorInvalidHexString",
  201.     kQ3ErrorWroteMoreThanSize, "\pkQ3ErrorWroteMoreThanSize",
  202.     kQ3ErrorWroteLessThanSize, "\pkQ3ErrorWroteLessThanSize",
  203.     kQ3ErrorReadLessThanSize, "\pkQ3ErrorReadLessThanSize",
  204.     kQ3ErrorReadMoreThanSize, "\pkQ3ErrorReadMoreThanSize",
  205.     kQ3ErrorNoBeginGroup, "\pkQ3ErrorNoBeginGroup",
  206.     kQ3ErrorSizeMismatch, "\pkQ3ErrorSizeMismatch",
  207.     kQ3ErrorStringExceedsMaximumLength, "\pkQ3ErrorStringExceedsMaximumLength",
  208.     kQ3ErrorValueExceedsMaximumSize, "\pkQ3ErrorValueExceedsMaximumSize",
  209.     kQ3ErrorNonUniqueLabel, "\pkQ3ErrorNonUniqueLabel",
  210.     kQ3ErrorEndOfContainer, "\pkQ3ErrorEndOfContainer",
  211.     kQ3ErrorUnmatchedEndGroup, "\pkQ3ErrorUnmatchedEndGroup",
  212.     kQ3ErrorFileVersionExists, "\pkQ3ErrorFileVersionExists",
  213.     /* View errors */
  214.     kQ3ErrorViewNotStarted, "\pkQ3ErrorViewNotStarted",
  215.     kQ3ErrorViewIsStarted, "\pkQ3ErrorViewIsStarted",
  216.     kQ3ErrorRendererNotSet, "\pkQ3ErrorRendererNotSet",
  217.     kQ3ErrorRenderingIsActive, "\pkQ3ErrorRenderingIsActive",
  218.     kQ3ErrorImmediateModeUnderflow, "\pkQ3ErrorImmediateModeUnderflow",
  219.     kQ3ErrorDisplayNotSet, "\pkQ3ErrorDisplayNotSet",
  220.     kQ3ErrorCameraNotSet, "\pkQ3ErrorCameraNotSet",
  221.     kQ3ErrorDrawContextNotSet, "\pkQ3ErrorDrawContextNotSet",
  222.     kQ3ErrorNonInvertibleMatrix, "\pkQ3ErrorNonInvertibleMatrix",
  223.     kQ3ErrorRenderingNotStarted, "\pkQ3ErrorRenderingNotStarted",
  224.     kQ3ErrorPickingNotStarted, "\pkQ3ErrorPickingNotStarted",
  225.     kQ3ErrorBoundsNotStarted, "\pkQ3ErrorBoundsNotStarted",
  226.     kQ3ErrorDataNotAvailable, "\pkQ3ErrorDataNotAvailable",
  227.     kQ3ErrorNothingToPop, "\pkQ3ErrorNothingToPop",
  228.     /* Renderer Errors */
  229.     kQ3ErrorUnknownStudioType, "\pkQ3ErrorUnknownStudioType",            /* TODO Name Change */
  230.     kQ3ErrorAlreadyRendering, "\pkQ3ErrorAlreadyRendering",
  231.     kQ3ErrorStartGroupRange, "\pkQ3ErrorStartGroupRange",
  232.     kQ3ErrorUnsupportedGeometryType, "\pkQ3ErrorUnsupportedGeometryType",
  233.     kQ3ErrorInvalidGeometryType, "\pkQ3ErrorInvalidGeometryType",
  234.     kQ3ErrorUnsupportedFunctionality, "\pkQ3ErrorUnsupportedFunctionality",
  235.     /* Group Errors */
  236.     kQ3ErrorInvalidPositionForGroup, "\pkQ3ErrorInvalidPositionForGroup",
  237.     kQ3ErrorInvalidObjectForGroup, "\pkQ3ErrorInvalidObjectForGroup",
  238.     kQ3ErrorInvalidObjectForPosition, "\pkQ3ErrorInvalidObjectForPosition",
  239.     /* Transform Errors */
  240.     kQ3ErrorScaleOfZero, "\pkQ3ErrorScaleOfZero",                /* Transform errors? */
  241.     /* String Errors */
  242.     kQ3ErrorBadStringType, "\pkQ3ErrorBadStringType",                /* What is this? */
  243.     /* Attribute Errors */
  244.     kQ3ErrorAttributeNotContained, "\pkQ3ErrorAttributeNotContained",        /* What is this? */
  245.     kQ3ErrorAttributeInvalidType, "\pkQ3ErrorAttributeInvalidType",        /* What is this? */
  246.     /* Camera Errors */
  247.     kQ3ErrorInvalidCameraValues, "\pkQ3ErrorInvalidCameraValues",        /* Redundant with kQ3ErrorParameterOutOfRange */
  248.     /* DrawContext Errors */
  249.     kQ3ErrorBadDrawContextType, "\pkQ3ErrorBadDrawContextType",
  250.     kQ3ErrorBadDrawContextFlag, "\pkQ3ErrorBadDrawContextFlag",
  251.     kQ3ErrorBadDrawContext, "\pkQ3ErrorBadDrawContext",
  252.     kQ3ErrorUnsupportedPixelDepth, "\pkQ3ErrorUnsupportedPixelDepth"
  253. } ;
  254.  
  255.  
  256.  
  257.  
  258. static void ConcatenateString( Str255 firstString, Str255 addThis ) ;
  259. static void CopyString( Str255 firstString, Str255 fromThis ) ;
  260. static void PresentErrMsgInDialog( Str255 theError ) ;
  261.  
  262.  
  263. //-------------------------------------------------------------------------------------------
  264. // MyErrorHandler - handle warnings from QuickDraw 3d
  265. void MyErrorHandler( TQ3Error sticky, TQ3Error latest, long refCon )
  266. {
  267. #pragma unused (refCon)
  268.     Str255     myErrorStr ;
  269.     Str255    theWholeString ;
  270.  
  271.     short    stickyIndex = sticky - kQ3ErrorInternalError ;
  272.     short    latestIndex = latest - kQ3ErrorInternalError ;
  273.     
  274.     // if the error is out of range, then just tell us that something
  275.     // "unknown" happened. Otherwise look up the error from the table.
  276.     if( sticky != kQ3ErrorNone && ( sticky < kQ3ErrorInternalError || sticky > kQ3ErrorUnsupportedPixelDepth )) {
  277.         CopyString(    theWholeString, stringTable[kErrorString].errorString  ) ;
  278.         ConcatenateString( theWholeString, stringTable[ kUnknownErrorString ].errorString ) ;
  279.     }
  280.     else {
  281.         
  282.         
  283.         CopyString(    theWholeString, stringTable[ kErrorString ].errorString  ) ;
  284.         
  285.         ConcatenateString( theWholeString, stringTable[ kFirstErrorString ].errorString ) ;
  286.         CopyString(    myErrorStr, errorTable[ stickyIndex ].theString) ;
  287.         ConcatenateString( theWholeString, myErrorStr) ;
  288.         
  289.         ConcatenateString( theWholeString, stringTable[ kLastErrorString ].errorString ) ;
  290.         CopyString(    myErrorStr, errorTable[ stickyIndex ].theString) ;
  291.         ConcatenateString( theWholeString, myErrorStr) ;
  292.         
  293.         
  294.         // and display the error string 
  295.         PresentErrMsgInDialog( theWholeString ) ;        
  296.     }
  297.     
  298.     // clear down the error
  299.     latest = Q3Error_Get( &sticky );
  300.     
  301. }
  302.  
  303.  
  304. //-------------------------------------------------------------------------------------------
  305. // MyErrorHandler - handle warnings from QuickDraw 3d
  306. void MyWarningHandler( TQ3Warning     sticky, TQ3Warning     latest, long refCon )
  307. {
  308. #pragma unused (refCon)
  309.     Str255     myWarningStr ;
  310.     Str255    theWholeString ;
  311.  
  312.     short    stickyIndex = sticky - kQ3WarningInternalException ;
  313.     short    latestIndex = latest - kQ3WarningInternalException ;
  314.     
  315.     // if the error is out of range, then just tell us that something
  316.     // "unknown" happened. Otherwise look up the error from the table.
  317.     if( sticky != kQ3ErrorNone && ( sticky < kQ3WarningInternalException || sticky > kQ3WarningParameterOutOfRange )) {
  318.     
  319.         CopyString(    theWholeString, stringTable[ kWarningString ].errorString  ) ;
  320.         ConcatenateString( theWholeString, stringTable[ kUnknownErrorString ].errorString ) ;
  321.         
  322.     }
  323.     else {
  324.         
  325.         CopyString(    theWholeString, stringTable[ kWarningString ].errorString  ) ;
  326.         
  327.         ConcatenateString( theWholeString, stringTable[ kFirstErrorString ].errorString ) ;
  328.         CopyString(    myWarningStr, warningTable[ stickyIndex ].theString) ;
  329.         ConcatenateString( theWholeString, myWarningStr) ;
  330.         
  331.         ConcatenateString( theWholeString, stringTable[ kLastErrorString ].errorString ) ;
  332.         CopyString(    myWarningStr, warningTable[ stickyIndex ].theString) ;
  333.         ConcatenateString( theWholeString, myWarningStr) ;
  334.         
  335.         // and display the error string 
  336.         PresentErrMsgInDialog( theWholeString ) ;        
  337.     }
  338.     
  339.     // clear down the error
  340.     latest = Q3Warning_Get( &sticky );
  341.     
  342.  
  343. }
  344.  
  345. //------------------------------------------------------------------------
  346. static void ConcatenateString( Str255 firstString, Str255 addThis )
  347. {
  348.     BlockMove( &addThis[1], &firstString[firstString[0]+1], addThis[0] ) ;
  349.     firstString[0] += addThis[0] ;
  350. }
  351.  
  352.  
  353. //------------------------------------------------------------------------
  354. static void CopyString( Str255 firstString, Str255 fromThis )
  355. {
  356.     BlockMove( &fromThis[1], &firstString[1], fromThis[0] ) ;
  357.     firstString[0] = fromThis[0] ;
  358. }
  359.  
  360. //------------------------------------------------------------------------
  361.  
  362. static void PresentErrMsgInDialog( Str255 theError )
  363. {    
  364.     short    itemHit ;
  365.     ParamText( theError, 0L,  0L,  0L ) ;
  366.     itemHit = StopAlert( kErrorHandlerAlertID, nil ) ;
  367.     
  368.     if( itemHit != ok ) 
  369.         ExitToShell() ;
  370. }